import os
import unittest
import sys
import subprocess
import logging
import datetime

from Config.RunConfig import RunConfig, Options
from Autotests.BaseTest import BaseManservSOAPTestCase
from Autotests.Suites.StressTest.Statistics import Statistics

class WorkersRunner(BaseManservSOAPTestCase):
    def __init__(self, *args, **kwargs):
        BaseManservSOAPTestCase.__init__(self, *args, **kwargs)

    def test_RunWorkers(self):
        Statistics.init()
        appPath = os.path.realpath(os.path.join(os.path.dirname(sys.executable), 'WorkerProcess.exe'))
        mailerPath = os.path.realpath(os.path.join(os.path.dirname(sys.executable), 'MailerProcess.exe'))

        pipes = []
        for processId in range(0, int(RunConfig().get(Options.StressTest.workersNumber))):
            pipe = subprocess.Popen(appPath)
            pipes.append(pipe)

        mailerPipe = subprocess.Popen(mailerPath)
        pipes.append(mailerPipe)

        while pipes:
            pipe = pipes.pop()
            pipe.wait()

if __name__ == "__main__":
    unittest.main()
